<?php

include('./datePicker.php');
include('./timePicker.php');

class Event {

    function addEvent() {

    echo '<form method="POST" action="addEventPageHandler.php">' . "\n";

    echo "<fieldset><legend>Please fill out the following information to add an event.</legend>\n";

    echo '<label for="EventID">EventID:</label><input type="text" size="3" name="EventID" /><br /><br />' . "\n";
    echo '<label for="EventName">Name:</label><input type="text" size="50" name="EventName" /><br /><br />' . "\n";
    echo '<label for="Price">Price:</label><input type="text" size="10" name="Price" /><br /><br />' . "\n";
    echo '<label for="Date">Date:</label>';
    
    	$date = new DatePicker();
    
    	$date->chooseDate();
    
    echo '<br /><br />' . "\n";
    
    echo '<label for="Time">Time:</label>';
    
    	$time = new timePicker();
    	
    	$time->chooseTime();
    
    echo '<br /><br />' . "\n";

    $dbConn = odbc_connect('EventDB','MyUser','Temp123');

    if (!$dbConn) {
      exit("Connection Failed: " . $dbConn);
    }
    	
    $sql = "SELECT VenueID, VenueName FROM VENUE;";

    $rs = odbc_exec($dbConn,$sql);

    if (!$rs) {
      exit("Error in SQL");
    }

    echo '<label for="VenueID">Venue:</label><select name="VenueID">' . "\n";

    while(odbc_fetch_row($rs)) {
      $VenueID  = odbc_result($rs,"VenueID"); 
      $VenueName = odbc_result($rs, "VenueName");
                  
      echo '<option value="' . $VenueID . '">' . $VenueName . "</option>\n";
    }   
      
    echo "</select>\n";  

    echo "<br /><br />\n";

    $sql = "SELECT Ad_ID FROM Advertising;";

    $rs = odbc_exec($dbConn, $sql);

    echo '<label for="Ad_ID">Advertising ID:</label><select name="Ad_ID">' . "\n";

    while(odbc_fetch_row($rs)) {
      $Ad_ID  = odbc_result($rs,"Ad_ID"); 
                  
      echo '<option value="' . $Ad_ID . '">' . $Ad_ID . "</option>\n";
    }   
      
    echo "</select>\n";  

    echo "<br /><br />\n";

    $sql = "SELECT SponsorID, SponsorName FROM Sponsors;";

    $rs = odbc_exec($dbConn, $sql);

    echo '<label for="SponsorID">Sponsor Name:</label><select name="SponsorID">' . "\n";

    while(odbc_fetch_row($rs)) {
      $SponsorID  = odbc_result($rs,"SponsorID");
      $SponsorName = odbc_result($rs, "SponsorName");
                  
      echo '<option value="' . $SponsorID . '">' . $SponsorName . "</option>\n";
    }   
      
    echo "</select><br /><br />\n"; 

    echo '<input class="buttonS" type="submit" value="Submit" name="B1" /><input class="buttonR" type="reset" value="Reset" name="B2" /></fieldset></form>';

    odbc_free_result($rs);

    odbc_close($dbConn);

    }

    function addEventHandler() {

        $EventID=$_POST['EventID'];
        $EventName=$_POST['EventName'];
        $Price=$_POST['Price'];
        $Year=$_POST['Year'];
        $Month=$_POST['Month'];
        $Day=$_POST['Day'];
        $Hour=$_POST['Hour'];
        $Minute=$_POST['Minute'];
        $Meridiem=$_POST['Meridiem'];
        $VenueID=$_POST['VenueID'];
        $Ad_ID=$_POST['Ad_ID'];
        $SponsorID=$_POST['SponsorID'];
        $Date = $Year . '-' . $Month . '-' . $Day;
		$Time = $Hour . ':' . $Minute . ':00 ' . $Meridiem;


        $dbConn = odbc_connect("EventDB","MyUser","Temp123");
        if (!$dbConn)
          {
          die('Could not connect: ' . odbc_error());
          }
          
        $sql = "INSERT INTO Events VALUES($EventID, '$EventName', $Price, $Date, $Ad_ID, $SponsorID, $VenueID, $Time)";

        $rs = odbc_exec($dbConn, $sql);

        odbc_close($dbConn);

        echo "<fieldset><legend>You entered the following Event Information</legend>\n";

        echo '<span class="label">EventID:</span><span class="output">' . $EventID . "</span><br /><br />\n";
        echo '<span class="label">Name:</span><span class="output">' . $EventName . "</span><br /><br />\n";
        echo '<span class="label">Price:</span><span class="output">' . $Price . "</span><br /><br />\n";
        echo '<span class="label">Date:</span><span class="output">' . $Date . "</span><br /><br />\n";
        echo '<span class="label">Time:</span><span class="output">' . $Time . "</span><br /><br />\n";

        $sql = "SELECT VenueName FROM Venue WHERE VenueID = $VenueID";

        $rs = odbc_exec($dbConn, $sql);

        while(odbc_fetch_row($rs))
        {
          $VenueName = odbc_result($rs, "VenueName");
          
          echo '<span class="label">Venue Name (ID):</span><span class="output">' . $VenueName . " ($VenueID)</span><br /><br />\n";
        }

        $sql = "SELECT SponsorName FROM Sponsors WHERE SponsorID = $SponsorID;";

        $rs = odbc_exec($dbConn, $sql);

        while(odbc_fetch_row($rs))
        {
          $SponsorName = odbc_result($rs, "SponsorName");
          
          echo '<span class="label">Sponsor Name (ID):</span><span class="output">' . $SponsorName . " ($SponsorID)</span><br /><br />\n";
        }

        echo '<span class="label">Advertising ID:</span><span class="output">' . $Ad_ID . "</span><br /><br />\n";

        echo "</fieldset>\n";

        odbc_free_result($rs);

        odbc_close($dbConn);
    }
}

?>